import tkinter as tk
import serialPort
import tcpip

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.pack()
        self.createWidgets()
    
    def createWidgets(self):
        self.btnNewTcpClient = tk.Button(self, text ='新建TCP', command=self.showFrmTcp)
        self.btnNewTcpClient.pack()

        self.btnNewSerialPort = tk.Button(self, text ='新建串口', command=self.showFrmSerialPort)
        self.btnNewSerialPort.pack()
    
    def showFrmTcp(self):
        frmTcp = tk.Toplevel(self)
        frmTcp.lblIp = tk.Label(frmTcp, text='IP地址：')
        frmTcp.lblIp.pack()
        strIp = tk.StringVar()
        self.txtIp = tk.Entry(frmTcp, textvariable=strIp)
        strIp.set('106.14.139.28')
        self.txtIp.pack()
        
        frmTcp.lblPort = tk.Label(frmTcp, text='端口号：')
        frmTcp.lblPort.pack()
        strPort = tk.StringVar()
        self.txtPort = tk.Entry(frmTcp, textvariable=strPort)
        strPort.set('22')
        self.txtPort.pack()

        frmTcp.lblClientNum = tk.Label(frmTcp, text='客户端数量：')
        frmTcp.lblClientNum.pack()
        strClientNum = tk.StringVar()
        self.txtClientNum = tk.Entry(frmTcp, textvariable=strClientNum)
        strClientNum.set('1')
        self.txtClientNum.pack()

        self.strBtnText = tk.StringVar()
        self.strBtnText.set('开启')
        frmTcp.btnTcpSwitch = tk.Button(frmTcp, textvariable=self.strBtnText, command=self.btnTcpSwitchClick)
        frmTcp.btnTcpSwitch.pack()

    def btnTcpSwitchClick(self):
        strTemp = '开启'
        if self.strBtnText.get() == strTemp:
            ip = self.txtIp.get()
            port = int(self.txtPort.get())
            clientNum = int(self.txtClientNum.get())
            tcpip.newTcpClient(ip, port, clientNum)
            self.strBtnText.set('关闭')
        else:
            clientNum = int(self.txtClientNum.get())
            tcpip.CloseTcpClient(clientNum)
            self.strBtnText.set('开启')
        
    def showFrmSerialPort(self):
        frmSerialPort = tk.Toplevel(self)
        frmSerialPort.lblPort = tk.Label(frmSerialPort, text='串口号：')
        frmSerialPort.lblPort.pack()
        self.txtIp = tk.Entry(frmSerialPort)
        self.txtIp.pack()


app = Application()

app.mainloop()
